package com.songjia.springcloud.config;

import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.ExecutorType;
import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 
 * 
 * @Description: Mybatis自定义配置
 *
 * @author: songjia
 * @date: 2018年8月11日 上午10:56:50
 */
@Configuration
public class MybatisConfig {

	// 当前环境
	@Value("${spring.profiles.active:default}")
	private String profile;

	/**
	 * 自定义MyBatis的配置规则；给容器中添加一个ConfigurationCustomizer；
	 * 
	 * @return
	 */
	@Bean
	public ConfigurationCustomizer mybatisConfigurationCustomizer() {
		return new ConfigurationCustomizer() {

			@Override
			public void customize(org.apache.ibatis.session.Configuration configuration) {
				configuration.setCacheEnabled(Boolean.TRUE);
				configuration.setLazyLoadingEnabled(Boolean.TRUE);
				configuration.setAggressiveLazyLoading(Boolean.TRUE);
				configuration.setUseGeneratedKeys(Boolean.TRUE);
				configuration.setDefaultExecutorType(ExecutorType.SIMPLE);
				configuration.setMapUnderscoreToCamelCase(Boolean.TRUE);// 开启驼峰映射
				if (!profile.equalsIgnoreCase("prod")) { // 非生产环境打印SQL语句
					configuration.setLogImpl(StdOutImpl.class);
				}
			}
		};

	}
}
